/**
 * 
 */
package gisc.gov.vn.dev.dataManager;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import javax.swing.JOptionPane;

/**
 * @author HT
 *
 */
public class GiscQueryFilter implements IGiscQueryFilter {

	private ArrayList<String> arrUserInfo = new ArrayList<String>();
	private String sql;
	private static Connection conn;
	private static Statement statement;
	private ResultSet result=null;
	private int intRowResult;
	
	public GiscQueryFilter()
	{
	}
	public GiscQueryFilter(String sqlStatement)
	{
		this.sql=sqlStatement;
	}
	
	public GiscQueryFilter(String sqlStatement,String userName,String pwd, String database, String driver, int resultType, int resultConcurency)
	{
		this.sql=sqlStatement;
		try {
			Class.forName(driver);
			conn=DriverManager.getConnection(database,userName,pwd);
			statement=conn.createStatement(resultType, resultConcurency);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}


	/* (non-Javadoc)
	 * @see gisc.gov.vn.dev.dataManager.IGiscQueryFilter#Excute(java.lang.String)
	 */
	@Override
	public void Excute() {
		// TODO Auto-generated method stub
//		Runnable runn= new Runnable() {
//			public void run() {
//				try {
//					result = statement.executeQuery(sql);
//				} catch (SQLException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
//			}
//		};
//		Thread th = new Thread(runn);
//		th.start();
		
		try {
			result = statement.executeQuery(sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, e);
		}
	}


	/* (non-Javadoc)
	 * @see gisc.gov.vn.dev.dataManager.IGiscQueryFilter#GetResult()
	 */
	@Override
	public ResultSet GetResult() {
		// TODO Auto-generated method stub
		
		return this.result;
	}

	/* (non-Javadoc)
	 * @see gisc.gov.vn.dev.dataManager.IGiscQueryFilter#Close()
	 */
	@Override
	public void Close() {
		try {
			this.statement.close();
			this.conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		// TODO Auto-generated method stub
	}
	/* (non-Javadoc)
	 * @see gisc.gov.vn.dev.dataManager.IGiscQueryFilter#ExcuteNonResult()
	 */
	@Override
	public void ExcuteNonResult() {
		// TODO Auto-generated method stub
		try {
			intRowResult=statement.executeUpdate(this.sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/* (non-Javadoc)
	 * @see gisc.gov.vn.dev.dataManager.IGiscQueryFilter#GetRowCount()
	 */
	@Override
	public int GetRowCount() {
		// TODO Auto-generated method stub
		return intRowResult;
	}


}
